package com.avengers.core.model.generator;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.generator.BaseModelGenerator;
import com.jfinal.plugin.activerecord.generator.Generator;
import com.jfinal.plugin.activerecord.generator.ModelGenerator;

import javax.sql.DataSource;

/**
 * @author yu.wang
 * @since 2018-04-03 上午10:46
 */
public class AvengersModelGenerator extends Generator {
    public AvengersModelGenerator(DataSource dataSource, String baseModelPackageName, String baseModelOutputDir, String modelPackageName, String modelOutputDir) {
        this(dataSource, new AvengersBaseModelGenerator(modelPackageName, baseModelPackageName, baseModelOutputDir), new ModelGenerator(modelPackageName, baseModelPackageName, modelOutputDir));
    }

    public AvengersModelGenerator(DataSource dataSource, BaseModelGenerator baseModelGenerator, ModelGenerator modelGenerator) {
        super(dataSource, baseModelGenerator, modelGenerator);
        this.metaBuilder = new AvengersMetaBuilder(dataSource);
        this.setBaseModelTemplate("template/base_model_template.jf");
        this.setMappingKitTemplate("template/mapping_kit_template.jf");
        this.setModelTemplate("template/model_template.jf");
    }

    @Override
    public void generate() {
        super.generate();
    }

    public void addIncludeTables(String... includeTables) {
        ((AvengersMetaBuilder) this.metaBuilder).addIncludeTables(includeTables);
    }

    /**
     * model开头
     */
    public void setModelNameStart(String modelNameStart) {
        ((AvengersMetaBuilder) this.metaBuilder).setModelNameStart(modelNameStart);
    }

    public void setModelNameEnd(String modelNameEnd) {
        ((AvengersMetaBuilder) this.metaBuilder).setModelNameEnd(modelNameEnd);
    }

    public <M extends Model<M>> void setBaseModelClass(Class<M> clazz) {
        ((AvengersBaseModelGenerator) this.baseModelGenerator).setBaseModelName(clazz.getSimpleName());
        ((AvengersBaseModelGenerator) this.baseModelGenerator).setBaseModelImport(clazz.getPackage().getName());
    }
}
